package com.leador.module;


import android.util.Log;

import com.leador.location.LocationTrace;
import com.leador.utils.JsonTool;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public  class ILocation implements Serializable {
    /**
     * 本地标识Id
     */
    private String id;
    /**
     * 用户的ak，授权使用
     */
    private String ak;
    /**
     * servicede ID，作为其唯一标识
     */
    private String service_id;
    /**
     * 经度
     */
    private String longitude = "0.0";
    /**
     * 纬度
     */
    private String latitude= "0.0";
    /**
     * entity唯一标识
     */
    private String entity_name;

    private String coord_type ="1";
    /**
     * 时间
     */
    private String loc_time = null;

    //n米/s
    private String speed="0";
    //状态(行驶或者停留)
    private String status="0";
    //扩展字段
    private String extend1;
    //扩展字段
    private String extend2;
    private String isGps;
    public int accuracy = 0;
    public int bearing = 0;
    /**
     * 用户的ak，授权使用
     * @return
     */
    public String getAk() {
        return ak;
    }

    public void setAk(String ak) {
        this.ak = ak;
    }

    /**
     * servicede ID，作为其唯一标识
     * @return
     */
    public String getService_id() {
        return service_id;
    }

    public void setService_id(String service_id) {
        this.service_id = service_id;
    }

    public String getLongitude() {
        return longitude;
    }

    /**
     * 设置经度
     * @param longitude
     */
    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getLatitude() {
        return latitude;
    }

    /**
     * 设置纬度
     * @param latitude
     */
    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    /**
     * 获取用户实例
     * @return
     */
    public String getEntity_name() {
        return entity_name;
    }

    /**
     * 设置用户实例
     * @param entity_name
     */
    public void setEntity_name(String entity_name) {
        this.entity_name = entity_name;
    }

    /**
     *
     * @return
     */
    public String getCoord_type() {
        return coord_type;
    }

    public void setCoord_type(String coord_type) {
        this.coord_type = coord_type;
    }

    /**
     * 获取时间
     * @return
     */
    public String getLoc_time() {
        return loc_time;
    }

    /**
     * 设置时间
     * @param loc_time
     */
    public void setLoc_time(String loc_time) {
        this.loc_time = loc_time;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSpeed() {
        return speed;
    }

    public void setSpeed(String speed) {
        this.speed = speed;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getExtend1() {
        return extend1;
    }

    public void setExtend1(String extend1) {
        this.extend1 = extend1;
    }

    public String getExtend2() {
        return extend2;
    }

    public void setExtend2(String extend2) {
        this.extend2 = extend2;
    }

    public String getIsGps() {
        return isGps;
    }

    public void setIsGps(String isGps) {
        this.isGps = isGps;
    }

    public int getAccuracy() {
        return accuracy;
    }

    public void setAccuracy(int accuracy) {
        this.accuracy = accuracy;
    }

    public int getBearing() {
        return bearing;
    }

    public void setBearing(int bearing) {
        this.bearing = bearing;
    }

    public static List<String[]> jsonToStringArray(String json){
        List<String[]> list = new ArrayList<String[]>();
        if(json == null){
            return list;
        }

        if(json.substring(0,4).equals("null")){
            json = json.substring(5,json.length());
        }
        String ss = json.substring(0,json.length()-1);
        String ssJson = "["+ss+"]";

       try{
           JSONArray jsonArray = new JSONArray(ssJson);
           if(jsonArray!=null&&jsonArray.length()>0){
               for(int n=0;n<jsonArray.length();n++){
                   JSONObject jsonObject = jsonArray.getJSONObject(n);
                   String[] str = new String[4];
                   str[1] = jsonObject.getString("longitude");
                   str[2] = jsonObject.getString("latitude");
                   str[3] = jsonObject.getString("loc_time");
                   str[4] = "1";
                   list.add(str);
               }
           }
       }catch (Exception e){

       }

        return list;
    };

    public static String toJson(ILocation track){
        long time = Long.valueOf(track.getLoc_time());
        String custom = track.getExtend1();
        String json = "{\"ak\":\""+track.getAk()+"\"," +
                "\"service_id\":\""+track.getService_id()+"\"," +
                "\"entity_name\":\""+track.getEntity_name()+"\"," +
                "\"latitude\":\""+track.getLatitude()+"\"," +
                "\"longitude\":\""+track.getLongitude()+"\"," +
                "\"coord_type\":\""+track.getCoord_type()+"\"," +
                "\"loc_time\":\""+ String.valueOf(time)+"\"," +
                "\"extend1\":\""+custom+"\""+
                "}";
        return json;
    }

    public static String socketBeanToJson(ILocation iLocation){
        long time = Long.valueOf(iLocation.getLoc_time());
        String custom = iLocation.getExtend1();
        String json = "{\"ak\":\""+iLocation.getAk()+"\"," +
                "\"service_id\":\""+iLocation.getService_id()+"\"," +
                "\"entity_name\":\""+iLocation.getEntity_name()+"\"," +
                "\"latitude\":\""+iLocation.getLatitude()+"\"," +
                "\"longitude\":\""+iLocation.getLongitude()+"\"," +
                "\"coord_type\":\""+iLocation.getCoord_type()+"\"," +
                "\"loc_time\":\""+ String.valueOf(time)+"\"";
        if(custom != null && !custom.equals("")){
            String[] strArray = custom.split(";");
            for(String str : strArray){
                String[] single = str.split("-");
                json += ","+ "\""+single[0]+"\"" +":"+"\""+single[1]+"\"";
            }
        }
        json += "}";
        return json;
    };

    public static List<ILocation> jsonToList(String json){
        List<ILocation> iLocationList = new ArrayList<ILocation>();
        if(json == null || json.length() < 0){
            return iLocationList;
        }
        try {
            json = "["+json+"]";
            Log.e("json is",json);
            JSONArray jsonArray = new JSONArray(json);
            if(jsonArray!=null&&jsonArray.length()>0){
                for(int n=0;n<jsonArray.length();n++){
                    JSONObject jsonObject = jsonArray.getJSONObject(n);
                    String tempJson = jsonObject.toString();
                    ILocation iLocation = jsonToBean(tempJson);
                    iLocationList.add(iLocation);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return iLocationList;
    };

    public static ILocation jsonToBean(String json) {
        if(json == null || json.equals("")){
            return null;
        }
        LocationTrace locationTrace = JsonTool.toBean(json, LocationTrace.class);
        if(locationTrace == null){
            return null;
        }
        ILocation iLocation = new ILocation();
        iLocation.setAk(AppInfo.key);
        iLocation.setEntity_name(AppInfo.entityName);
        iLocation.setService_id(String.valueOf(AppInfo.serviceId));
        iLocation.setLoc_time(String.valueOf(locationTrace.getTime()));
        iLocation.setLongitude(String.valueOf(locationTrace.getLongitude()/1000000000.0D));
        iLocation.setLatitude(String.valueOf(locationTrace.getLatitude()/1000000000.0D));
        iLocation.setSpeed(String.valueOf(locationTrace.getSpeed()));
        iLocation.setAccuracy(locationTrace.getAccuracy()/100);
        iLocation.setBearing(locationTrace.getBearing()*2);
        iLocation.setExtend1(locationTrace.getExtend1());
        return iLocation;
    }

    @Override
    public String toString() {
        return "ILocation{" +
                "id='" + id + '\'' +
                ", ak='" + ak + '\'' +
                ", service_id='" + service_id + '\'' +
                ", longitude='" + longitude + '\'' +
                ", latitude='" + latitude + '\'' +
                ", entity_name='" + entity_name + '\'' +
                ", coord_type='" + coord_type + '\'' +
                ", loc_time='" + loc_time + '\'' +
                ", speed='" + speed + '\'' +
                ", status='" + status + '\'' +
                ", extend1='" + extend1 + '\'' +
                ", extend2='" + extend2 + '\'' +
                ", isGps='" + isGps + '\'' +
                ", accuracy=" + accuracy +
                ", bearing=" + bearing +
                '}';
    }
}
